define(['jquery', 'bootstrap', 'form', 'upload'], function ($, undefined, Form, Upload) {
    var Controller = {
        index: function () {
            Controller.api.bindevent();
            Controller.events.count_char();
            Upload.events.uploadSuccess=function (file, response, input_id, preview_id, uploader) {
                if (response.code == 1) {
                    var input_obj = $("#" + input_id);
                    var input_value = input_obj.val();
                    var multiple = uploader.options.pick.multiple;
                    var data = {suffix: file.ext, url: response.data.save_name, src: response.data.save_name};
                    uploader.makeThumb(file, function (error, src) {
                        if (error) {
                            data.src = '';//如果无法创建 则代表不是图片类型文件
                        }
                        if (multiple) {
                            var html = template.render(Upload.config.preview_tpl, data);//多选则重新追加图片预览
                            $("#" + preview_id).append(html);
                        } else {
                            $("#" + preview_id).attr('src', src);
                        }
                    }, 146, 146);
                    if (multiple) {
                        //最终赋值
                        input_value = input_value == '' ? [] : input_value.split(',');
                        input_value.push(response.data.save_name);
                    } else {
                        input_value = response.data.save_name;
                    }
                    input_obj.val(input_value);
                } else {
                    this.uploadError(file, response, input_id, preview_id, uploader)
                }
            },
            Upload.api.webuploader();

        },
        api: {
            bindevent: function () {
                var form = $('form[role="form"]');

                Form.api.bindevent(form);
            }
        },
        events: {
            count_char: function () {
                $("#remark").on("input focus keyup", function () {
                    let strs = $(this).val();
                    let length = strs.length;
                    let num = 500 - length;
                    if (num <= 0) {
                        num = 0;
                        $(this).val(strs.substr(0, 500));
                    }
                    $("small span").html(num);
                });
                $("#remark").trigger('input');
            }

        }
    };
    return Controller;
});
